package edu.wpi.cs509.ui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import edu.wpi.cs509.common.exceptions.ServerUnreachable;
import edu.wpi.cs509.ui.intfc.PlayerStatsInterface;

/**
 * Panel that is used to display personal stats
 * 
 * @author jprocess
 *
 */
@SuppressWarnings("serial")
public class LeaderboardPanel extends JPanel{
	A__MainStrategoClient client;
	JPanel leaderPanel = new JPanel();
	JScrollPane leaderPane = new JScrollPane();
	int currentRank;
	
	public LeaderboardPanel (A__MainStrategoClient client) {
		//set initial values
		this.client = client;
		client.setLeaderPanel(this);
		currentRank = 1;
		
		leaderPanel.setLayout(new BoxLayout(leaderPanel, BoxLayout.Y_AXIS));
		
		//create buttons
		JButton upButton = new JButton("^");
		JButton downButton= new JButton("v");
		JButton myRankButton = new JButton("My Rank");
		
		//add button listeners 
		upButton.addActionListener(new upButtonAction());
		downButton.addActionListener(new downButtonAction());
		myRankButton.addActionListener(new myRankButtonAction());
		
		//leaderPane.add(leaderPanel);
		
		setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
		
		//request the first list for leaderboard
		try {
			//Grab position 0 and the 24 people after it. IE: the top 25 players.
			client.getClientLogic().requestLeaderBoard(0, 24);
		} catch (ServerUnreachable e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		//add ui components
		add(upButton);
		add(leaderPanel);
		add(downButton);
		add(myRankButton);
	}
	
	/**
	 * updates the leader board with the given statsList
	 * @param statsList - list of player stats to show
	 */
	public void UpdateLeaderboard (List<PlayerStatsInterface> statsList) {
		leaderPanel.removeAll();
		leaderPanel.setVisible(false);
		
		for(PlayerStatsInterface player : statsList) {
			JLabel playerLabel = new JLabel(player.getRank() + "  " + player.getUsername());
			leaderPanel.add(playerLabel);
		}	
		
		leaderPanel.setVisible(true);
		validate();
		repaint();
	}
	
	//action called when the up button is pressed
	//requests the previous 25 users
	private class upButtonAction implements ActionListener {
		upButtonAction() {	
		}
		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				currentRank -= 25;
				if(currentRank < 1) {
					currentRank = 1;
				}
				client.getClientLogic().requestLeaderBoard(currentRank, 25);
			} catch (ServerUnreachable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	//action called when down button is pressed
	//requests the next 25 users
	private class downButtonAction implements ActionListener {
		downButtonAction() {	
		}
		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				currentRank += 25;
				client.getClientLogic().requestLeaderBoard(currentRank, 25);
			} catch (ServerUnreachable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	//action called when My Rank button is pressed
	//requests my rank and the following 25 players 
	private class myRankButtonAction implements ActionListener {
		myRankButtonAction() {	
		}
		@Override
		public void actionPerformed(ActionEvent arg0) {
			try {
				client.getClientLogic().requestLeaderBoard(client.getUserSession().getUsername(), 25);
			} catch (ServerUnreachable e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}